/**
 * 商品规格处理
 */

import {
    Matrix
} from "./matrix.js"

import {
    Fence
} from "./fence.js"

import {
    CellStatus
} from "../../core/enum.js"

class FenceGroup {
    spu
    skuList = [] // 处理前的规格列表
    fences = [] //处理后的fences
    constructor(spu) {
        this.spu = spu
        this.skuList = spu.sku_list
    }

    /**
     * 方法一
     * 创建矩阵二维数组
     * 将数据转置
     */
    initFences() {
        // 创建矩阵 二维数组
        const newArr = this._carateMatrix(this.skuList)
        let transArr = newArr.transpose()
        const fences = []
        transArr.forEach(item => {
            const fence = new Fence(item)
            // 这里只是一个fence
            fence.init()
            // 将每个fence添加fences
            fences.push(fence)
        })
        this.fences = fences
    }

    /**
     * 方法二
     * 创建矩阵二维数组
     * 遍历数组拿到每一个元素和行列号
     * 发现当遍历的数据列改变的时候 就是一个分类的结束
     */
    initFences1() {
        const newArr = this._carateMatrix(this.skuList)
        const fences = []
        let currentCol = -1
        newArr.each((item, r, c) => {
            //    返回的列不等于当前的列=>开启了一个新列
            if (currentCol !== c) {
                currentCol = c
                let newFence = this._createFence()
                // 将每列添加入fences
                fences[c] = newFence
            }
        })
    }

    // 获取每个cell
    eachCell(cb) {
        for (let i = 0; i < this.fences.length; i++) {
            for (let j = 0; j < this.fences[i].cells.length; j++) {
                const cell = this.fences[i].cells[j]
                cb(cell, i, j)
            }
        }

    }

    /**
     * 创建矩阵
     * 定义二维数组 [[1,2,3],[2,3,4]]
     */
    _carateMatrix(skuList) {
        const arr = []
        skuList.forEach(item => {
            arr.push(item.specs)
        })

        // 需要返回类里面的数据和方法
        return new Matrix(arr)
    }

    // 创建一个新fence
    _createFence() {
        const fence = new Fence()
        return fence
    }
}

export {
    FenceGroup
}